﻿#include <thirdparty/fist-log.h>

#include <list>

#include "svc_conf.h"
#include "svc_info.h"


CBoxParser::CBoxParser()
    : m_cfg(new BoxConf())
{
    
}
CBoxParser::~ CBoxParser()
{
    
}
bool CBoxParser::Parser(XMLElement* root)
{
    m_cfg->services.clear();
    auto box = root->FirstChildElement();
    while (box) {
        std::string name = box->Name();
        int type = box->IntAttribute("type");
        std::pair<int, std::unordered_map<int, BoxInfo>> ins;
        ins.first = type;
        for (auto info = box->FirstChildElement("list")->FirstChildElement(); info != nullptr; 
            info = info->NextSiblingElement()) {
            BoxInfo box_info;
            box_info.name = name;
            box_info.type = type;
            box_info.id = info->IntAttribute("id");
            box_info.ip = info->Attribute("ip");
            box_info.listen = info->IntAttribute("listen");
            box_info.tick = info->IntAttribute("tick");
            if (!ins.second.insert({ box_info.id , box_info}).second) {
                LOG_FATAL("service duplication. name:{}, id:{}", name, box_info.id);
                return false;
            }
        }
        if (!ins.second.empty()) {
            m_cfg->services.emplace(ins);
        } else {
            LOG_WARN("empty service type. name:{}", name);
        }
        box = box->NextSiblingElement();
    }
    
    return !m_cfg->services.empty();
}

const BoxConf* CBoxParser::cfg()
{
    return m_cfg;
}
